package com.example.nettyclient.netty;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : zjs
 * @Date: 2021/4/29 18:16
 * Description:
 */

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * Netty客户端
 **/
@Slf4j
public class NettyClientUtil {

    public static String helloNetty(String msg) {
//    public static ResponseResult helloNetty(String msg) {
        NettyClientHandler nettyClientHandler = new NettyClientHandler();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
//        自定义分隔符%010d
            msg = msg + "%010d";
            //以自定义分隔符作为消息的分隔
            ByteBuf delimiter = Unpooled.copiedBuffer("%010d".getBytes());
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    //该参数的作用就是禁止使用Nagle算法，使用于小数据即时传输
                    .option(ChannelOption.TCP_NODELAY, true)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, delimiter));
                            socketChannel.pipeline().addLast("decoder", new StringDecoder());
                            socketChannel.pipeline().addLast("encoder", new StringEncoder());
                            socketChannel.pipeline().addLast(nettyClientHandler);
                        }
                    });

            ChannelFuture future = bootstrap.connect("127.0.0.1", 16777).sync();
            log.info("客户端发送成功....");
            //发送消息
            future.channel().writeAndFlush(msg);
            // 等待连接被关闭
            future.channel().closeFuture().sync();
            return future.channel().attr(AttributeKey.valueOf("Attribute_key")).get().toString();
//            return nettyClientHandler.getResponseResult();
        } catch (Exception e) {
            log.error("客户端Netty失败" + e.getMessage());
            throw new RuntimeException(e.getMessage());
        } finally {
            //以一种优雅的方式进行线程退出
            group.shutdownGracefully();
        }
    }
}
